package prj.carmap.mobile.activity;

import com.loopj.android.http.AsyncHttpClient;
import com.miaowang.salad.activity.UserLoginActivity;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.process.LoginProcess;
import prj.carmap.mobile.process.UpdateVersionTask;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.widget.EditText;

/**
 * 启动屏
 * 
 */
public class SplashActivity extends Activity {

	final static String TAG = "SplashActivity";

	/** 活动判断 */
	protected boolean _active = true;

	/** 停留时间 */
	protected int _splashTime = 1000;

	private SharedPreferences mPreferences;
	static AsyncHttpClient client = new AsyncHttpClient();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_splash);
		CarmapApplication.addActivity(this);
		CarmapApplication.SERVER_URL = this.getString(R.string.server_url);

		// 版本更新检查
		new UpdateVersionTask(this).checkVersion();

		// 分类信息加载
		/*
		 * SellerTypeProcess typeProcess = new SellerTypeProcess();
		 * typeProcess.setContext(this); typeProcess.setFetchOnly(true);
		 * typeProcess.getSellerTypes();
		 */

		mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

		// 延迟进程
		Thread splashTread = new Thread() {
			@Override
			public void run() {
				try {
					int waited = 0;
					while (_active && (waited < _splashTime)) {
						sleep(100);
						if (_active) {
							waited += 100;
						}
					}
				} catch (InterruptedException e) {
				} finally {
					// 启动主应用
					SplashActivity.this.findViewById(R.id.splash_auto_image)
							.post(new Runnable() {
								public void run() {
									SplashActivity.this
									.startActivity(new Intent(
											SplashActivity.this,
											UserLoginActivity.class));
									
									
//									String cardNo = mPreferences.getString(
//											"cardNo", "");
//									String password = mPreferences.getString(
//											"password", "");
//									String phoneNum = mPreferences.getString(
//											"phoneNum", "");
//									String txtModels = mPreferences.getString(
//											"txtModels", "");
//									String txtLicense = mPreferences.getString(
//											"txtLicense", "");
//									// SplashActivity.this.startActivity(new
//									// Intent(SplashActivity.this,
//									// AutoActivity.class));
//									if (cardNo.equals("")|| password.equals("")|| phoneNum.equals("")) {
//										SplashActivity.this
//												.startActivity(new Intent(
//														SplashActivity.this,
//														UserLoginActivity.class));
//									} else {
//										LoginProcess p = new LoginProcess();
//										p.setContext(SplashActivity.this);
//										p.login(cardNo, password, phoneNum,
//												txtModels, txtLicense);
//										//SplashActivity.this.finish();
//									}
								}
							});
				}
			}
		};
		splashTread.start();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 如果有触摸事件，则跳过等待
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			_active = false;
		}

		return true;
	}

}
